package com.xueyi.operations.controller.admin;

import com.xueyi.common.core.web.result.AjaxResult;
import com.xueyi.common.core.web.validate.V_A;
import com.xueyi.common.core.web.validate.V_E;
import com.xueyi.common.log.annotation.Log;
import com.xueyi.common.log.enums.BusinessType;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import com.xueyi.operations.controller.base.BOpMaterialController;
import com.xueyi.operations.domain.dto.OpMaterialDto;
import com.xueyi.operations.domain.query.OpMaterialQuery;
import io.swagger.v3.oas.annotations.Operation;
import com.xueyi.common.security.annotation.AdminAuth;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 资料管理管理 | 管理端 业务处理
 *
 * @author lwb
 */
@AdminAuth
@RestController
@RequestMapping("/admin/material")
public class AOpMaterialController extends BOpMaterialController {

    @Override
    @GetMapping("/list")
    @Operation(summary = "查询资料管理列表")
    @PreAuthorize("@ss.hasAuthority('RD:operations:material:list')")
    public AjaxResult list(OpMaterialQuery opMaterial) {
        return super.list(opMaterial);
    }

    @Override
    @GetMapping(value = "/{id}")
    @Operation(summary = "查询资料管理详细")
    @PreAuthorize("@ss.hasAuthority('RD:operations:material:single')")
    public AjaxResult getInfo(@PathVariable Serializable id) {
        OpMaterialDto dto = baseService.selectById(id);
        if(StringUtils.isNotEmpty(dto.getAttachment())){
            dto.setAttachmentArr(dto.getAttachment().split(","));
        }
        return success(dto);
    }

    @Override
    @PostMapping("/export")
    @Operation(summary = "资料管理导出")
    @PreAuthorize("@ss.hasAuthority('RD:operations:material:export')")
    @Log(title = "资料管理管理", businessType = BusinessType.EXPORT)
    public void export(HttpServletResponse response, OpMaterialQuery opMaterial) {
        super.export(response, opMaterial);
    }

    @Override
    @PostMapping
    @Operation(summary = "资料管理新增")
    @PreAuthorize("@ss.hasAuthority('RD:operations:material:add')")
    @Log(title = "资料管理管理", businessType = BusinessType.INSERT)
    public AjaxResult add(@Validated({V_A.class}) @RequestBody OpMaterialDto opMaterial) {

        if(opMaterial.getAttachmentArr() != null && opMaterial.getAttachmentArr().length != 0){
            String attchment = Arrays.stream(opMaterial.getAttachmentArr()).collect(Collectors.joining(","));
            opMaterial.setAttachment(attchment);
        }
        return super.add(opMaterial);
    }

    @Override
    @PutMapping
    @Operation(summary = "资料管理修改")
    @PreAuthorize("@ss.hasAuthority('RD:operations:material:edit')")
    @Log(title = "资料管理管理", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@Validated({V_E.class}) @RequestBody OpMaterialDto opMaterial) {
        return super.edit(opMaterial);
    }

    @Override
    @DeleteMapping("/batch/{idList}")
    @Operation(summary = "资料管理批量删除")
    @PreAuthorize("@ss.hasAuthority('RD:operations:material:del')")
    @Log(title = "资料管理管理", businessType = BusinessType.DELETE)
    public AjaxResult batchRemove(@PathVariable List<Long> idList) {
        return super.batchRemove(idList);
    }
}
